package com.esyoga.yoyoga.handler;

import com.esyoga.yoyoga.enums.UserTypeEn;
import com.esyoga.yoyoga.pojo.*;
import com.esyoga.yoyoga.service.*;
import com.esyoga.yoyoga.util.CutPagesUtil;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 一些通用的请求
 */
@Data
@Controller
@RequestMapping("/user")
public class UserHandler {
    @Resource
    StudentService studentService;
    @Resource
    TrainerService trainerService;
    @Resource
    GymService gymService;
    @Resource
    StudentDataService studentDataService;
    @Resource
    PayService payService;


    /**
     * 跳转到充值界面
     * @param request
     * @return
     */
    @RequestMapping("/toRechargePage")
    @ResponseBody
    public ModelAndView  toRechargePage(HttpServletRequest request){
        HttpSession session = request.getSession();
        UserTypeEn userTypeEn = (UserTypeEn) session.getAttribute("userTypeEn");
        Object user = session.getAttribute("user");
        BigDecimal balance=null;

        switch (userTypeEn){
            case STUDENT:
                Integer sId = ((TStudent)user).getsId();
                balance = studentDataService.showData(sId).get(0).getsBalance();
                break;
            case TRAINER:
                balance = trainerService.selectTrainerById(((TTrainer)user).gettId()).gettBanlance();
                break;
            case GYM:
                return  null;
            case PLATFORM:
                return null;
        }

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("balance",balance);
        //
        //
        //
        modelAndView.setViewName("html/recharge.html");
        return modelAndView;

    }

    /**
     * 查询当前用户的账户流水
     * @return
     */

    @RequestMapping("/selectBills")
    @ResponseBody
    public Map<String,Object> selectBill(HttpServletRequest request, Integer page){
        Map<String,Object> result=new HashMap<String,Object>();
        HttpSession session = request.getSession();
        Object user = session.getAttribute("user");
        UserTypeEn userTypeEn = (UserTypeEn) session.getAttribute("userTypeEn");
        Integer uId ;
        switch (userTypeEn){
            case STUDENT:
                TStudent student = (TStudent)user;
                uId = student.getsId();
                break;
            case TRAINER:
                TTrainer trainer = (TTrainer)user;
                uId = trainer.gettId();
                break;
            case PLATFORM:
                TPlatform platform = (TPlatform)user;
                uId = platform.getpId();
                break;
            case GYM:
                TGym gym = (TGym)user;
                uId = gym.getgId();
                break;
            default:
                return result;
        }
        CutPagesUtil.clear();
        CutPagesUtil.setPageInfo(page,10);
        List<TBill> bills =  payService.selectBillByUser(uId,userTypeEn.getCode());
        List<TBill> billList = CutPagesUtil.getPages(bills);
        int total = CutPagesUtil.getTotalPage();

        result.put("total",total);
        result.put("currentMessage",billList);
        return result;
    }

}
